package com.bootdo.payee.service.impl;

import static com.bootdo.common.utils.ShiroUtils.getUserId;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.config.Constant;
import com.bootdo.common.redis.lock.CommonRedisLock;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.mq.RabbitUtil;
import com.bootdo.mq.SendSortUtil;
import com.bootdo.payee.dao.PayeeDao;
import com.bootdo.payee.dao.PaymentCodeDao;
import com.bootdo.payee.domain.PayeeDO;
import com.bootdo.payee.domain.PaymentCodeDO;
import com.bootdo.payee.service.PaymentCodeService;
import com.bootdo.system.dao.UserRoleDao;

@Service
public class PaymentCodeServiceImpl implements PaymentCodeService{

    @Autowired
    private PaymentCodeDao paymentCodeMapper;

    @Autowired
    private UserRoleDao userRoleMapper;
    @Autowired
    protected StringRedisTemplate stringRedisTemplate;
    @Autowired
    private PayeeDao payeeMapper;
    @Autowired
    private RabbitUtil rabbitUtil;
    @Autowired
    private SendSortUtil sendSortUtil;


    /**
     * 获取收款码列表
     * @param map
     * @return
     */
    @Override
    public List<PaymentCodeDO> list(Map<String, Object> map){
        //获取用户权限
        int checkUserIsManager = userRoleMapper.checkUserIsManager(getUserId());
        if (checkUserIsManager != 1){
            map.put("userId", getUserId());
        }
        return paymentCodeMapper.list(map);
    }

    /**
     * 获取收款码列表总条数
     * @param map
     * @return
     */
    @Override
    public int count(Map<String, Object> map){
        //获取用户权限
        int checkUserIsManager = userRoleMapper.checkUserIsManager(getUserId());
        if (checkUserIsManager != 1){
            map.put("userIdCreate", getUserId());
        }
        return paymentCodeMapper.count(map);
    }



    @Override
    public PaymentCodeDO get(Long codeId){
        return paymentCodeMapper.get(codeId);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public int editReceivingAccount(Long codeId, Integer receivingAccount,Long userIdCreate) {
        PaymentCodeDO paymentCode = new PaymentCodeDO();
        paymentCode.setCodeId(codeId);
        paymentCode.setGmtModified(new Date());
        paymentCode.setReceivingAccount(receivingAccount);
        int flag = paymentCodeMapper.update(paymentCode);

        if(receivingAccount == 1 && flag > 0){
            PaymentCodeDO paymentCodeDO = new PaymentCodeDO();
            paymentCodeDO.setCodeId(codeId);
            paymentCodeDO.setGmtModified(new Date());
            paymentCodeDO.setUserIdCreate(userIdCreate);
            flag = paymentCodeMapper.updateOtherRecAcc(paymentCodeDO);
        }

        if (receivingAccount == 0) {
            PayeeDO payee = new PayeeDO();
            payee.setUserId(userIdCreate);
            payee.setLineUpStatus(0);
            payeeMapper.update(payee);
            paymentCode = paymentCodeMapper.get(codeId);

            CommonRedisLock commonRedisLock = new CommonRedisLock(stringRedisTemplate);
            boolean f = commonRedisLock.searchListByValue(Constant.PASSAGEWAY + paymentCode.getPassagewayId(), String.valueOf(paymentCode.getUserIdCreate()));
            if (f){
                commonRedisLock.lRemove(Constant.PASSAGEWAY + paymentCode.getPassagewayId(), 0, String.valueOf(paymentCode.getUserIdCreate()));
                JSONObject object = new JSONObject();
                String queueName = Constant.QUEUE_START+payee.getUserId();
                object.put("code",3);
                object.put("date", DateUtils.format(new Date(),DateUtils.DATE_TIME_PATTERN));
                object.put("userId",payee.getUserId());
                object.put("msg","收款码已被禁用，请联系管理员");
                object.put("payType", paymentCode.getPassagewayId());
                rabbitUtil.sendToQueue(queueName,object.toJSONString());
            }
            sendSortUtil.sendMsg(paymentCode.getPassagewayId());
        }

        return flag;
    }

    @Override
    public int updateAuditStatus(Long codeId, String auditStatus) {
        //先判断这个码是否被删除
        PaymentCodeDO paymentCode = paymentCodeMapper.get(codeId);
        if (paymentCode==null) {
            return -2;
        }
        return paymentCodeMapper.updateAuditStatus(codeId,auditStatus);
    }
}
